package com.boc.langxiang.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.CompoundButton;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.common.AddressManageContract;
import com.boc.langxiang.entity.response.AddressEntity;
import com.boc.langxiang.ui.activity.common.AddAddressActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * CreateTime: 2016-06-17上午9:23
 * Author: wjkjinke00@126.com
 * Description:
 */
public class AddressManageAdapter extends BaseQuickAdapter<AddressEntity> {

    private AddressManageContract.Presenter mPresenter;
    private Context context;
    private AddressManageContract.View mView;

    public AddressManageAdapter(Context context, List<AddressEntity> data, AddressManageContract.Presenter presenter
            , AddressManageContract.View view) {
        super(R.layout.item_address, data);
        mPresenter = presenter;
        this.context = context;
        mView = view;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final AddressEntity item) {
        String is_default = item.getIs_default();
        helper.setText(R.id.name, item.getUsername())
                .setText(R.id.phone, item.getPhone())
                .setText(R.id.address, item.getProvince() + item.getCity() + item.getDistrict() + item.getAddress())
                .setChecked(R.id.default_address, is_default.equals(AddressEntity.DEFAULT) ? true : false)
                .setOnCheckedChangeListener(R.id.default_address, new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            mView.setDefaultAddr(item.getId());
                        }
                    }
                })
                .setOnClickListener(R.id.edit, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, AddAddressActivity.class);
                        intent.putExtra("edit", true);
                        intent.putExtra("address", item);
                        context.startActivity(intent);
                    }
                })
                .setOnClickListener(R.id.delete, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.deleteAddress(item.getId());
                    }
                });
    }

}
